package estyle.auto.nkornilova.applogicImp;

import estyle.auto.nkornilova.model.Message;
import estyle.auto.nkornilova.util.BankAccountMessageXMLParser;
import org.openqa.selenium.Alert;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nkornilova
 * Date: 06.05.13
 * Time: 12:40
 * To change this template use File | Settings | File Templates.
 */
public class BankAccountMessageHelper extends DriverBasedHelper {

    public BankAccountMessageHelper(ApplicationManager applicationManager) {
        super(applicationManager.getWebDriver());
    }


    public boolean isVisible()
    {
        return pages.messagePage.isVisible();
    }

    public void createSuccessPaperBankAccountPrimaryMessageAboutCreating(Message message) throws InterruptedException {

        if(message!=null)
        {
            pages.messagePage
                    .ensurePageLoaded()
                    .moveToInnerFrame()
                    .moveToInsurerTab()
                    .setBankPMessageCheckBox(message.isBankPMessage())
                    .setPayerPMessageCheckBox(message.isPayerPMessage())
                    .setKindComboBox(message.getKind())
                    .setIdentifierField(message.getIdentifier())
                    .setMsgDateField(message.getMsgDate())
                    .setStateRegCertSerField(message.getStateRegCertSer())
                    .setStateRegCertNumField(message.getStateRegCertNum())
                    .setTaxRegCertSerField(message.getTaxRegCertSer())
                    .setTaxRegCertNumField(message.getTaxRegCertNum())
                    .selectInsurer()
                    .moveToInnerFrame()
                    .setRegNumField(message.getInsurerRegNum())
                    .setOgrnField(message.getInsurerOgrn())
                    .setInnField(message.getInsurerInn())
                    .setKppField(message.getInsurerKpp())
                    .sendKeyEnterForSearch("КПП")
                    .selectInsurerRecordOne()
                    .moveToOut()
                    .clickOnChooseButton()
                    .moveToBankTab()
                    .setTaxAuthBankField(message.getTaxAuthBank())
                    .selectBank()
                    .moveToInnerFrame()
                    .setBicField(message.getBankBic())
                    .setNumberField(message.getBankNumberAccount())
                    .setRegNumField(message.getBankRegNum())
                    .setFullNameField(message.getBankFullName())
                    .sendKeyEnterForSearch("РЕГНОМЕР")
                    .selectBankRecordOne()
                    .moveToOut()
                    .clickOnChooseButton()
                    .setBankInnField(message.getBankInn())
                    .setBankKppField(message.getBankKpp())
                    .setBankRepPositionField(message.getBankRepPosition())
                    .setBankPhoneField(message.getBankPhone())
                    .setBankRepFullNameField(message.getBankRepFullName())
                    .moveToAccountTab()
                    .setAccNumberField(message.getAccNumber())
                    .setContractStatusComboBox(message.getContractStatus())
                    .setContractCreationField(message.getContractCreation())
                    .setContractNumberField(message.getContractNumber())
                    .setAccStatusComboBox(message.getAccStatus())
                    .setAccCurrComboBox(message.getAccCurr())
                    .setAccTypeField(message.getAccType())
                    .setAccOpenDateField(message.getAccOpenDate())
                    .saveMessage();

        }
    }

    public boolean checkInsurerFieldAfterChosen(Message message)
    {
       return (pages.messagePage.getClientOgrnField().equals(message.getInsurerOgrn())
       && pages.messagePage.getClientInnField().equals(message.getInsurerInn())
       && pages.messagePage.getClientKppField().equals(message.getInsurerKpp())
       );
    }

    public Message getMessageFromXML(String fileXMLPath)
    {
        BankAccountMessageXMLParser xmlParser = new BankAccountMessageXMLParser();
        List<Message> messageList =
                xmlParser.getMessagesFromXML(fileXMLPath);
        return messageList.get(0);
    }

    public boolean isAlertExists()
    {
        Alert alert = driver.switchTo().alert();
        return alert!=null;
    }

    public String getAlertText()
    {
        Alert alert = driver.switchTo().alert();
        return alert.getText();
    }
}
